// This file is part of OpenMeca, an easy software to do mechanical simulation.
//
// Author(s)    :  - Damien ANDRE  <openmeca@gmail.com>
//
// Copyright (C) 2012 Damien ANDRE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.


#include <QApplication>
#include <iostream>

#include "OpenMeca/Gui/WidgetQuaternion.hpp"
#include "OpenMeca/Util/Dimension.hpp"
#include "OpenMeca/Util/Unit.hpp"

namespace OpenMeca
{
  namespace Gui
  {
    WidgetQuaternion::WidgetQuaternion(QWidget* parent)
      :QWidget(parent),
       WidgetInterface<Geom::Quaternion<_3D> >(*parent),
       axisValue_(0.,0.,0.),
       angleValue_(0.)
    {
      Ui::WidgetQuaternion::setupUi(this);
      axis_->SetLabel(QObject::tr("Axis"));
      axis_->SetDimension(Util::Dimension::Get("Length"));
      angle_->SetLabel(QObject::tr("Angle"));
      angle_->SetDimension(Util::Dimension::Get("Angle"));
    }

    WidgetQuaternion::~WidgetQuaternion()
    {
    }



    void 
    WidgetQuaternion::SetValue(Geom::Quaternion<_3D>& q)
    {
      WidgetInterface<Geom::Quaternion<_3D> >::SetValue(q);
      axisValue_  = q.GetAxis();
      angleValue_ = q.GetAngle();
      axis_->SetValue(axisValue_);      
      angle_->SetNumber(angleValue_);
    }


     void 
     WidgetQuaternion::Init()
    {
    }
    

    void 
    WidgetQuaternion::SetLabel(const QString& str)
    {
      groupBox_->setTitle(str);
    }
   
    bool
    WidgetQuaternion::Check()
    {
      bool ok = (axis_->Check()==true && angle_->CheckUserEntry(angleValue_));
      if (ok == false)
	return false;
      
      Geom::Quaternion<_3D> q(axis_->GetCopy(), angleValue_);
      WidgetInterface<Geom::Quaternion<_3D> >::GetCopy() = q;
      
      return true;

      // double x, y, z;
      // bool ok = (x_->CheckUserEntry(x) && y_->CheckUserEntry(y) && z_->CheckUserEntry(z));
      // if (ok)
      // 	{
      // 	  Geom::Quaternion<_3D> p(x, y, z);
      // 	  WidgetInterface<Geom::Quaternion<_3D> >::GetCopy() = p;
      // 	}
      // return ok;
    }

  }
}


